package logmgr;
import static Common.ByteArray.* ;
import Common.GoggleDB;
import storage.data.Page;
import storage.data.PageID;

public class IntRecord extends LogMgr {
	
	public int old, cur ;	
	
	public void redo (){ 
//		System.out.printf ( "REDO transaction:%d page:%d offset:%d from the value:%d to value:%d\n",
//		txnum, pageid,  pos, (Integer)old, (Integer)cur ) ;
		try {
			PageID pid = new PageID ( pageid ) ;
			Page aPage = new Page () ; 
			GoggleDB.bufferMgr.pinPage(pid, aPage, false) ;
			writeInt ( aPage.getData(), pos, cur ) ;
			GoggleDB.bufferMgr.unpinPage(pid, true ) ;
			GoggleDB.bufferMgr.flush( pid ) ;
		}catch ( Exception e ) {
		}
		
	} 
	
	public void undo () {
//		System.out.printf ( "UNDO transaction:%d page:%d offset:%d from the value:%d to value:%d\n",
//				txnum, pageid, pos, (Integer)old, (Integer)cur ) ;		
		//System.out.println ( "Int has been undo") ;
		try {
			PageID pid = new PageID ( pageid ) ;
			Page aPage = new Page () ; 
			GoggleDB.bufferMgr.pinPage(pid, aPage, false) ;
			writeInt ( aPage.getData(), pos, old ) ;
			GoggleDB.bufferMgr.unpinPage(pid, true ) ;
			GoggleDB.bufferMgr.flush( pid ) ;
		}catch ( Exception e ) {
		}
	}
	
	public void read () throws Exception {
		byte[] buff = new byte [ 20 ] ; 
		file.read( buff ) ;
		txnum = readInt ( buff,  0 ) ;
		pageid = readInt ( buff, 4 ) ;
		pos = readInt ( buff, 8 ) ;
		old = readInt ( buff, 12 ) ;
		cur = readInt ( buff, 16 ) ; 
	}
	
	@Override
	public void write ( int txnum, int pageid, int pos, Object old, Object cur ) throws Exception {
		byte [] buff = new byte [ 24 ] ;
		writeInt ( buff, 0, INT ) ;
		writeInt ( buff, 4, txnum ) ;
		writeInt ( buff, 8, pageid ) ;
		writeInt ( buff, 12, pos ) ;
		writeInt ( buff, 16, (Integer)old ) ;
		writeInt ( buff, 20 ,(Integer)cur) ;
		file.seek( file.length() ) ;
		file.write ( buff ) ; 
	}

}
